package com.hawk.eye.server.controller.hawkeye;

import com.hawk.eye.common.core.ApiResult;
import com.hawk.eye.framework.utils.CollectionUtil;
import com.hawk.eye.common.page.PageQuery;
import com.hawk.eye.common.page.PageResult;
import com.hawk.eye.server.convert.TemplateConvert;
import com.hawk.eye.server.domain.dto.resp.TemplateRespDTO;
import com.hawk.eye.server.domain.vo.req.TemplateReqVO;
import com.hawk.eye.server.domain.vo.resp.TemplateRespVO;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.hawk.eye.framework.core.controller.BaseController;
import com.hawk.eye.server.service.ITemplateService;

import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author salad
 * @date 2023-09-28
 */
@RestController
@RequestMapping("/hawk-eye/template")
@RequiredArgsConstructor
public class TemplateController extends BaseController {

    private final ITemplateService templateService;

    /**
     * 模板配置列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:template:query')")
    @GetMapping("/template-list")
    public ApiResult<List<TemplateRespVO>> templateList() {
        List<TemplateRespDTO> templates = templateService.templateList();
        return ApiResult.ok(TemplateConvert.INSTANCE.to(templates));
    }

    /**
     * 默认模板 todo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:template:query')")
    @GetMapping("/default-template")
    public ApiResult<List<TemplateRespVO>> defaultTemplate(){
        return ApiResult.ok(TemplateConvert.INSTANCE.to(templateService.defaultTemplate()));
    }

    /**
     * 分页查询模板配置
     * @param pageQuery
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:template:query')")
    @GetMapping("/page")
    public PageResult<TemplateRespVO> page(PageQuery pageQuery, TemplateReqVO reqVO) {
        PageResult<TemplateRespDTO> page = templateService.page(pageQuery, TemplateConvert.INSTANCE.to(reqVO));
        return PageResult.of(page,TemplateConvert.INSTANCE::to);
    }

    /**
     * 模板配置详情
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:template:query')")
    @GetMapping("/detail/{id}")
    public ApiResult<TemplateRespVO> detail(@PathVariable("id") Long id) {
        TemplateRespDTO dto = templateService.getTemplate(id);
        return ApiResult.ok(TemplateConvert.INSTANCE.to(dto));
    }

    /**
     * 保存模版配置
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:template:add')")
    @PostMapping("/save")
    public ApiResult save(@RequestBody @Validated TemplateReqVO reqVO) {
        boolean status = templateService.save(TemplateConvert.INSTANCE.to(reqVO));
        return ApiResult.status(status);
    }

    /**
     * 复制模板配置
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:template:add')")
    @PostMapping("/copy")
    public ApiResult copy(@RequestBody @Validated TemplateReqVO reqVO) {
        boolean status = templateService.save(TemplateConvert.INSTANCE.to(reqVO));
        return ApiResult.status(status);
    }


    /**
     * 修改模板配置
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:template:edit')")
    @PutMapping("/update")
    public ApiResult update(@RequestBody @Validated TemplateReqVO reqVO) {
        boolean status = templateService.update(TemplateConvert.INSTANCE.to(reqVO));
        return ApiResult.status(status);
    }

    /**
     * 删除模板配置
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:template:remove')")
    @DeleteMapping("/remove/{ids}")
    public ApiResult remove(@PathVariable Long[] ids) {
        return ApiResult.status(templateService.removeBatchByIds(CollectionUtil.toList(ids)));
    }

}
